package news.network;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import inter.Delegate;
import news.dto.SimpleNotify;

/**
 * 获取新闻
 * Created by Administrator on 2016/3/23.
 */
public class GetNewNotify {

    Delegate delegate;
    StringRequest stringRequest;

    RequestNotify requestNotify;


    public void setRequestNotify(RequestNotify requestNotify) {
        this.requestNotify = requestNotify;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String ip = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + ip + "/Notify/GetNewNotify";
        stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("获取新闻", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data", gson.toJson(requestNotify));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }


    public static class RequestNotify {
        private String notify_ID;
        private String isNew;

        public String getNotify_ID() {
            return notify_ID;
        }
        public void setNotify_ID(String notify_ID) {
            this.notify_ID = notify_ID;
        }
        public String getIsNew() {
            return isNew;
        }
        public void setIsNew(String isNew) {
            this.isNew = isNew;
        }
    }

    public static class NotifyStatus {
        private static final String NOPARA  = "0";	// 参数为空
        private static final String SUCCESS = "1";	// 成功
        private static final String NOMSGS  = "2";  // 没有最新消息
        private static final String FAILED = "3";	// 业务逻辑出错
    }

    public static class ResponseNotify {
        private String status;
        private List<List<String>> notifyLists;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public List<List<String>> getNotifyLists() {
            return notifyLists;
        }

        public void setNotifyLists(List<List<String>> notifyLists) {
            this.notifyLists = notifyLists;
        }
    }
}
